

package com.cn.项目.飞机预约管理系统.UserFrame;

import com.cn.项目.utils.JdbcUtils;
import com.cn.项目.飞机预约管理系统.Maininterface.Fail;
import com.cn.项目.飞机预约管理系统.Maininterface.Success;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;


public class Recharge extends JFrame {
    public Recharge() {
        initComponents();
    }

    //确认充值
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        Connection conn = null;
        PreparedStatement st  = null;
        PreparedStatement st1 = null;
        ResultSet rs = null;
        String id = textField1.getText();
        String updateMoney = textField2.getText();
        String money = null;
        try {
            conn = JdbcUtils.getConnection();
            System.out.println("id:"+id);

            String sql1 ="select  *from users where id=?";
            st1 = conn.prepareStatement(sql1);
            st1.setString(1, id);
            rs = st1.executeQuery();
            if (rs.next()){
                money = rs.getString("money");
            }

            System.out.println(money);
            System.out.println(updateMoney);

            String newMoney= String.valueOf(Integer.parseInt(money)+Integer.parseInt(updateMoney));

            String sql = "update users set money=? where id=?";
            st = conn.prepareStatement(sql);
            st.setString(1,newMoney);
            st.setString(2,id);
            int i = st.executeUpdate();
            if (i>0){
                new Success();
            }else {
                new Fail();
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st , rs);
            try {
                st1.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setVisible(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("请您充值");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 12f));
        label1.setForeground(Color.red);
        contentPane.add(label1);
        label1.setBounds(125, 5, 210, 45);

        //---- label2 ----
        label2.setText("账号");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        contentPane.add(label2);
        label2.setBounds(25, 60, 75, 35);
        contentPane.add(textField1);
        textField1.setBounds(110, 65, 145, textField1.getPreferredSize().height);

        //---- label3 ----
        label3.setText("元");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        label3.setForeground(Color.red);
        contentPane.add(label3);
        label3.setBounds(225, 145, 65, 22);

        //---- label4 ----
        label4.setText("充值金额：");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 6f));
        contentPane.add(label4);
        label4.setBounds(30, 140, 95, 30);
        contentPane.add(textField2);
        textField2.setBounds(145, 140, 65, textField2.getPreferredSize().height);

        //---- button1 ----
        button1.setText("确认");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.setForeground(new Color(0, 153, 153));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(135, 190, 85, 40);

        contentPane.setPreferredSize(new Dimension(385, 275));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
